<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;

/**
 * 流程定义
 *
 */
class UFlow extends Backend
{
    /**
     * UFlow模型对象
     * @var \app\admin\model\UFlow
     */
    protected $model = null;
    protected $detail = null;
    protected $quickSearchField = ['FLOWID'];

    protected $defaultSortField = 'FLOWID,desc';

    protected $preExcludeFields = [''];
    protected $noNeedLogin      = ['*'];
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\UFlow;
        $this->detail = new \app\admin\model\UFlowdetail;
    }
    /**
     * 查看
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field('u_flow.*,DEPARTMENT.DEPTNAME')
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->join('DEPARTMENT',"u_flow.flowdept=DEPARTMENT.DEPTID")
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit)->toArray();
        $flowid=array_column($res['data'],'FLOWID');
        $flowdata= $this->detail
            ->alias('d')
            ->field('d.detailid,d.flowid,d.period,d.stagename,d.processflag,u.userid,u.username,u.id')
            ->where('flowid','in',$flowid)
            ->join('u_flowdetailusers u','d.detailid=u.detailid','left')
            ->select()->toArray();
        foreach ($res['data'] as &$v) {
            foreach ($flowdata as $a) {
                if($v['FLOWID']==$a['flowid']){
                    $v['two'][]=$a;
                }
            }
        }
        $this->success('', [
            'list'   => $res['data'] ,
            'total'  => $res['total'] ,
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 添加
     */
    public function add()
    {


        if ($this->request->isPost()) {

            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }
            $adminInfo          = $this->auth->getInfo();
            $data['OPNAME']=$adminInfo['nickname'];
            $data['OPDATETIME']=date('Y-m-d H:i:s',time());
            $result = false;
            Db::startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('add');
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Added successfully'),$this->model->FLOWID);
            } else {
                $this->error(__('No rows were added'));
            }
        }
        $this->error(__('Parameter error'));
    }
}